// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
    extends: cc.Component,
    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        cooling_time: 0,
        plant_prefab: {
            type: cc.Prefab,
            default: null,
        },
        plant_frame: {
            type: cc.SpriteFrame,
            default: null,
        },
        plant_area: {
            type: cc.Node,
            default: null,
        },
    },
    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        //console.log(this);
        //this.node.on('touchstart',this.onTouchStart,this);
        this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        //this.node.on('touchstart',this.onTouchOff,this);
        this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
        //this.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
    },
    start() {
        this.cd = this.node.getChildByName("cd");
        //console.log(this.plant_area.children[0].getBoundingBoxToWorld());
    },
    plant_generate: function(mousePoint) {
        var plant = cc.instantiate(this.plant_prefab);
        var pos = this.plant_area.convertToNodeSpaceAR(mousePoint);
        //console.log(pos);
        plant.name = "planting";
        plant.cd = this.cd;
        plant.x = pos.x; //mousePoint.x;
        plant.y = pos.y; //mousePoint.y;
        this.plant_area.addChild(plant);
    },
    onMouseDown: function(event) {
        let mouseType = event.getButton();
        if (mouseType === cc.Event.EventMouse.BUTTON_LEFT) {
            // 鼠标左键按下
            var mousePoint = event.getLocation();
            if (!this.cd.active) {
                this.plant_generate(mousePoint);
                this.cd_time = this.cooling_time; //设置cd时间
            }
        }
    },
    onTouchMove: function(event) {
        //console.log('鼠标移动了');
        if (!this.cd.active) {
            var mousePoint = cc.v2(event.getLocationX(), event.getLocationY());
            var pos = this.plant_area.convertToNodeSpaceAR(mousePoint);
            this.plant_area.getChildByName("planting").x = pos.x;
            this.plant_area.getChildByName("planting").y = pos.y;
        }
    },
    onMouseUp: function(event) {
        let mouseType = event.getButton();
        if (mouseType === cc.Event.EventMouse.BUTTON_LEFT) {
            // 鼠标左键释放
            /*this.cd.active = true; //cd效果开启
            this.cd_time = this.cooling_time; //启用cd节点*/
            /*let mousePoint = event.getLocation();
            let localPoint = this.node.convertToNodeSpace(mousePoint);*/
        }
    },
    update(dt) {
        if (this.cd.active) {
            this.cd_time -= dt;
            this.cd.getComponent(cc.ProgressBar).progress = this.cd_time / this.cooling_time;
            if (this.cd_time <= 0) {
                this.cd.active = false;
            }
        }
    },
});